﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace KeyboardCount
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            if (!CheckUsername())
            {
                return;
            }
            if (!CheckPassword())
            {
                return;
            }

            try
            {
                SendAndRecrive sendRcv = new SendAndRecrive();
                
                TcpClient tcpClient = new TcpClient(sendRcv.IP, sendRcv.SRV_PORT);
                sendRcv.SendUsernameAndPasswd(tcpClient, textBoxUserName.Text, textBoxPassword.Text, SendAndRecrive.State.REGISTER);
                
                if (sendRcv.errno == 0)
                {
                    MessageBox.Show("注册成功！");
                    tcpClient.Close();
                    this.Close();
                }
                else if (sendRcv.errno == 1)
                {
                    MessageBox.Show("用户名已存在！");
                }
                else
                {
                    MessageBox.Show("注册失败！");
                }
                tcpClient.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("注册失败！" + ex.Message);
            }
        }

        public bool CheckUsername()
        {
            string username = textBoxUserName.Text;
            if (username == "")
            {
                MessageBox.Show("请输入用户名！");
                textBoxUserName.Focus();
                return false;
            }
            else if (username.Length < 5 || username.Length > 20)
            {
                MessageBox.Show("用户名必须是5-20位字符！");
                textBoxUserName.Focus();
                return false;
            }

            for (int i = 0; i < username.Length; i++ )
            {
                if ((username[i] >= '0' && username[i] <= '9') ||
                    (username[i] >= 'a' && username[i] <= 'z') ||
                    (username[i] >= 'A' && username[i] <= 'Z') ||
                    username[i] == '_')
                {
                    continue;
                }
                else
                {
                    MessageBox.Show("用户名必须是字母、数字、下划线的字符！");
                    textBoxUserName.Focus();
                    return false;
                }
            }

            return true;
        }

        public bool CheckPassword()
        {
            string password = textBoxPassword.Text;

            if (password == "")
            {
                MessageBox.Show("请输入密码！");
                textBoxPassword.Focus();
                return false;
            }
            else if (password.Length < 5 || password.Length > 20)
            {
                MessageBox.Show("密码必须是5-20位字符！");
                textBoxPassword.Focus();
                return false;
            }

            for (int i = 0; i < password.Length; i++)
            {
                if ((password[i] >= '0' && password[i] <= '9') ||
                    (password[i] >= 'a' && password[i] <= 'z') ||
                    (password[i] >= 'A' && password[i] <= 'Z') ||
                    password[i] == '_')
                {
                    continue;
                }
                else
                {
                    MessageBox.Show("密码必须是字母、数字、下划线的字符！");
                    textBoxPassword.Focus();
                    return false;
                }
            }

            if (textBoxPassword.Text != textBoxQueRenPassword.Text)
            {
                MessageBox.Show("两次输入的密码不一致！");
                return false;
            }

            return true;
        }
    }
}
